package editdomain;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use base 'func';
use Data::Validate::Domain qw(is_domain);
use strict;
use CGI::Carp qw(fatalsToBrowser);

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub return_edit_domain {

    my $self = shift;

    my $dd;

    if (   ( !length( $self->{p}->{domain} ) )
        || ( 4 > length( $self->{p}->{domain} ) )
        || ( 250 < length( $self->{p}->{domain} ) )
        || !is_domain( $self->{p}->{domain} ) )
    {

        $self->{he}->{NO_DOMAIN} = 1;

    }
    else {

        my @sql;
        my $cl;

        if ( $self->{session}->{name} eq 'Admin' ) {
            push @sql, $self->{p}->{domain};
        }
        else {
            push @sql, $self->{p}->{domain}, $self->{session}->{user};
            $cl = 'AND idnane = ?';
        }

        my $sth = $self->{dbh}->prepare(
            "SELECT aliases,
                                domain,
                                mailboxes,
                                description,
                                maxquota,
                                backupmx,
                                modified,
                                active
                         FROM domain                     
                         WHERE domain = ?
                         $cl
                         LIMIT 1
                          "
        );
        $sth->execute(@sql);

        $dd = $sth->fetchrow_hashref();
        $sth->finish();
    }

    if ( $dd->{maxquota} > 0 ) {
        $dd->{maxquota} = $dd->{maxquota} / 1048576;
        $dd->{maxquota} = int( $dd->{maxquota} );
    }

    if ( $dd < 1 ) {
        $self->{he}->{NOTS} = 1;
    }
    else {

        while ( my ( $key, $value ) = each( %{$dd} ) ) {

            $self->{he}->{$key} = $value;

        }
    }

    return $self;
}

sub templ_hash_domain {
    my $self = shift;

    $self->{session}->{filet} = 'edit-domain';
    $self->SUPER::templ_hash(@_);
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
